import { defineStore } from 'pinia'
import {examApi} from '../api/exam'
export const useExamStore = defineStore('exam', {
    state: () => ({
        userInfo: null,
        exams: [],
        currentExam: {
            questions: [],
            answers: {},
            remainingTime: 1800 // 30分钟
        }
    }),
    actions: {
        async login(credentials) {
            // Axios模拟登录
            examApi.login(credentials).then(res=>{
                console.log(res,666);
                localStorage.setItem('exam_token',res.token)
                this.userInfo = res.userInfo
                this.exams = res.exams
            })
        },
        async initExam() {
            // 加载考试题目
            const { data } = await axios.get('/api/exam-questions')
            this.currentExam.questions = data
        },
        submitExam() {
            // 提交逻辑
            const score = calculateScore(this.currentExam.answers)
            this.exams = this.exams.map(exam =>
                exam.id === this.currentExam.id
                    ? { ...exam, status: '已完成', score }
                    : exam
            )
        }
    }
})